package com.wslds.blog.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wslds.base.action.BaseAction;
import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.BlogCons;
import com.wslds.base.util.Collects;
import com.wslds.base.util.Strings;
import com.wslds.bean.Review;
import com.wslds.blog.service.ReviewService;

@SuppressWarnings("serial")
@Component("reviewAction")
@Scope("prototype")
public class ReviewAction extends BaseAction{

	private Review review;
	private String url;
	
	@Resource
	private ReviewService reviewService;
	
	public String listReview(){
		
		if( this.getHttpRequest().getQueryString() != null ){
			this.setQueryString(Strings.cutPageQueryString(this.getHttpRequest().getQueryString()));
		}
		
		if( null == review ){
			review = new Review();
		}
		review.setExtraOrderColumns(" id desc ");
		
		PageAdapter<Review> adapter = reviewService.getDao().page("review", review, getPage());
		this.put("adapter", adapter);
		
		return SUCCESS;
	}
	@SuppressWarnings("all")
	public String saveReview() {
		try{
			List<Long> ids = reviewService.getDao().findExt("review_max_select", review.getArtId());
			if( Collects.notNull(ids) ){
				review.setSeq( ids.get(0) + 1);
			}else{
				review.setSeq( 1L );
			}
			
			if( Strings.isNull(review.getName())){
				review.setName(this.getHttpRequest().getRemoteAddr());
			}else{
				review.setName(review.getName());
				getSession().put(BlogCons.SESSION.ACCESS_NAME, review.getName());
			}
			
			if( Strings.notNull(review.getWebSite())){
				String webSite = review.getWebSite().replaceAll("http://", "");
				review.setWebSite("http://" + webSite);
				
				getSession().put(BlogCons.SESSION.ACCESS_URL, webSite);
			}
			
			if( Strings.notNull(review.getEmail())){
				review.setEmail(review.getEmail());
				getSession().put(BlogCons.SESSION.ACCESS_MAIL, review.getEmail());
			}
			
			if( Strings.isNull(review.getContent())){
				review.setContent("此人没力，内容居然都不填");
			}
			
			
			/**
			 * 转换 < >
			 * 限制最多15行
			 */
			String pureData = review.getContent();
			String[] splitData = pureData.split("\n");
			if( splitData.length > 15 ){
				StringBuffer sb = new StringBuffer();
				for( int i = 0; i < 15; i++ ){
					if( i < 14 ){
						sb.append(splitData[i] + "\n" );
					}
					else{
						sb.append(splitData[i]);
					}
				}
				pureData = sb.toString();
			}else if( pureData.trim().length() > 300 ) {
				pureData = pureData.trim().substring(0,300);
			}
			
			String simplePureData = "";
			if( pureData.length() > 50 ){
				simplePureData = pureData.substring(0,45) + "...";
			}else{
				simplePureData = pureData;
			}
			
			simplePureData = Strings.tranSmile(simplePureData);
			pureData = Strings.tranSmile(pureData);
			review.setContent(pureData);
			review.setSimpleContent(simplePureData);
			
			this.url = this.url.replaceAll("/wslds", "");
			review.setCreateDate(new Date());
			reviewService.getDao().save("review", review);
			
			getResponse().getWriter().write("OK");
			
		}catch (Exception e) {
			return ERROR;
		}
		
		return NONE;
	}
	
	
	public Review getReview() {
		return review;
	}

	public void setReview(Review review) {
		this.review = review;
	}

	public ReviewService getReviewService() {
		return reviewService;
	}

	public void setReviewService(ReviewService reviewService) {
		this.reviewService = reviewService;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}
	

}
